package com.wbdgj.ui.store.cafe;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.PointsMallAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.branch.NetfeeRoamingActivity;
import com.wbdgj.ui.home.MapDialogAtivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.store.IntegralRulesActivity;
import com.wbdgj.ui.store.MemberPointsActivity;
import com.wbdgj.ui.store.MissionCenterActivity;
import com.wbdgj.ui.store.PointsDetailsActivity;
import com.wbdgj.ui.store.PointsMallActivity;
import com.wbdgj.ui.store.PointsMallDetailsActivity;
import com.wbdgj.utils.MyGridView;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.views.AllRoundImageView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class StoreCofeFragment02 extends BaseFragment implements PtrHandler {

    private Context context;

    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @BindView(R.id.scrollView)
    ScrollView scrollView;

    @BindView(R.id.roundImg)
    AllRoundImageView roundImg;

    @BindView(R.id.jfTxt)
    TextView jfTxt;

    @BindView(R.id.demo_listview)
    MyGridView myPullListView;
    private PointsMallAdapter pointsMallAdapter;

    @Override
    protected int getLayout() {
        return R.layout.ui_story_cofe_fragment_02;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
        pfl_root.disableWhenHorizontalMove(true);
        getMbUserInfo();
        getMyIntegralInfoByApp();
        getHotGoodsList();

        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if (!pointsMallAdapter.hasNoData){
                    startActivity(new Intent(context, PointsMallDetailsActivity.class).putExtra("ID", decimalFormat.format(pointsMallAdapter.getItem(i).get("Id"))));
                }
            }
        });
    }

    private void getHotGoodsList() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getHotGoodsList(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        ArrayList<LinkedTreeMap> linkedTreeMapList = (ArrayList<LinkedTreeMap>)linkedTreeMap.get("data");
                        pointsMallAdapter = new PointsMallAdapter(context, linkedTreeMapList);
                        myPullListView.setAdapter(pointsMallAdapter);
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (!(linkedTreeMap.get("IMG")+"").equals("null")){
                        Glide.with(context).load(HttpAdapter.HEAD_URL+linkedTreeMap.get("IMG")).placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }else{
                        Glide.with(context).load("").placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }
                    userinfo(linkedTreeMap.get("ID_CARD")+"");
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    @BindView(R.id.level)
    TextView level;

    private void userinfo(String ID_CARD) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().userinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("code")){
                        if(decimalFormat.format(linkedTreeMap.get("code")).equals("200")){
                            LinkedTreeMap dataLink = (LinkedTreeMap) linkedTreeMap.get("data");
                            level.setText("当前处于"+dataLink.get("level")+"阶段");
                        }
                    }
                }
            }
        });
    }

    private void getMyIntegralInfoByApp() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMyIntegralInfoByApp(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID),BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        LinkedTreeMap dateLink = (LinkedTreeMap) linkedTreeMap.get("data");
                        if(dateLink.containsKey("AvailableIntegral")){
                            jfTxt.setText(decimalFormat.format(dateLink.get("AvailableIntegral")));
                        }
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void fetchData() {

    }

    @OnClick({R.id.morerwLay, R.id.jfmxLay, R.id.jfscLay, R.id.hyjfLay, R.id.hyjf02Lay, R.id.jfgz, R.id.qwc01Lay, R.id.qwc02Lay, R.id.qwc03Lay, R.id.moreSp})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.moreSp:
                launch(PointsMallActivity.class);
                break;
            case R.id.qwc01Lay:
                launch(MissionCenterActivity.class);
                break;
            case R.id.qwc02Lay:
                launch(MissionCenterActivity.class);
                break;
            case R.id.qwc03Lay:
                launch(MissionCenterActivity.class);
                break;
            case R.id.hyjfLay:
                launch(MemberPointsActivity.class);
                break;
            case R.id.hyjf02Lay:
                launch(MemberPointsActivity.class);
                break;
            case R.id.jfmxLay:
                launch(PointsDetailsActivity.class);
                break;
            case R.id.jfscLay:
                launch(PointsMallActivity.class);
                break;
            case R.id.morerwLay:
                launch(MissionCenterActivity.class);
                break;
            case R.id.jfgz:
                launch(IntegralRulesActivity.class);
                break;

        }
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        getMbUserInfo();
        getMyIntegralInfoByApp();
        getHotGoodsList();
        pfl_root.refreshComplete();
    }
}
